<template>
    <div>
        <navigation-bar>
            <div class="searchContent">
                <!-- <custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
                <custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button> -->
                <custom-button type="add" title="添加任务" style="margin-right: 10px" @addClick="addClick"></custom-button>
                <custom-button
                    type="custom"
                    icon="el-icon-video-pause"
                    title="暂停任务"
                    style="margin-right: 10px"
                    @customClick="customClick('1')"
                ></custom-button>
                <custom-button
                    type="custom"
                    icon="el-icon-position"
                    title="继续任务"
                    style="margin-right: 10px"
                    @customClick="customClick('2')"
                ></custom-button>
                <custom-button
                    type="custom"
                    icon="el-icon-error"
                    title="取消任务"
                    style="margin-right: 10px"
                    @customClick="customClick('3')"
                ></custom-button>
            </div>
        </navigation-bar>
        <table-index
            :dataSource="dataSource"
            :tableColumns="tableColumns"
            :btnList="btnList"
            :pageSize="pageSize"
            :pageCount="pageCount"
            :pageNo="pageNo"
            @pageChange="pageChange"
            @editRow="editRow"
            @viewRow="viewRow"
            @deleteRow="deleteRow"
            selection
            @handleSelectionChange="handleSelectionChange"
        ></table-index>

        <div v-if="dialogShow">
            <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
                <updateDevice @close="dialogShow = false"></updateDevice>
            </custom-dialog>
        </div>
    </div>
</template>

<script>
import {orgManageForm} from '@/assets/form/managementCenter/systemManage/orgManage.js'
import customDialog from '@/components/customComponents/customDialog'
import customButton from '@/components/customComponents/customForm/customButton'
import tableIndex from '@/components/customComponents/customeTable/tableIndex'
import navigationBar from '@/components/customComponents/managementCenter/navigationBar'
// import {AjaxCommon} from "@/libs/apiCommon"
import {CardId} from '@/libs/cardId'
import {PopupCommon} from '@/libs/popupCommon'
import {SessionCommon} from '@/libs/sessionCommon'
import {Utility} from '@/libs/utility'
import {deviceUpdateList} from '@/mockData/systemManage.js'
import updateDevice from './updateDevice'
export default {
    components: {
        navigationBar,
        customButton,
        tableIndex,
        customDialog,
        updateDevice
    },
    data() {
        return {
            selectDeviceList: [],
            unitName: '',
            device_type: '',
            device_typeList: Utility.getDictList('deviceType'),
            status: '',
            statusList: Utility.getDictList('status'),
            // table 相关参数  begin
            dataSource: deviceUpdateList,
            tableColumns: [
                {
                    prop: 'taskName',
                    label: '任务名称'
                },
                {
                    prop: 'updateFileName',
                    label: '升级文件'
                },
                {
                    prop: 'versionDate',
                    label: '版本日期'
                },
                {
                    prop: 'taskType',
                    label: '任务类型'
                },
                {
                    prop: 'updateRange',
                    label: '升级设备范围'
                },
                {
                    prop: 'startTime',
                    label: '升级开始时间'
                },
                {
                    prop: 'status',
                    label: '升级状态',
                    dictName: 'deviceStatus'
                }
            ],
            btnList: ['edit', 'delete'],
            pageSize: 20,
            pageCount: 4,
            pageNo: 1,
            // table 相关参数  end

            // 弹窗是否显示
            dialogShow: false,
            // 弹窗标题 (新增修改不同)
            dialogTitle: '',

            // form的配置信息
            formConfig: orgManageForm,
            type: 'add'
        }
    },

    methods: {
        customClick(type) {
            if (this.selectDeviceList.length < 1) {
                PopupCommon.notify('info', '请选择至少一个任务')
                return
            }
            PopupCommon.notify('success', type == 1 ? '暂停成功' : type == 2 ? '继续成功' : '取消成功')
        },

        handleSelectionChange(d) {
            this.selectDeviceList = d
        },

        formInputChange(e) {
            if (e.filedName == 'idCard') {
                const data = CardId.getInfoByCardId(e.value)
                if (data) {
                    let newData = e.allData
                    newData['birthday'] = data.birthday
                    newData['sex'] = data.sex
                    this.$refs.form.changeFormValue(newData)
                }
            }
        },
        submit(d) {
            // let self = this
            console.log(d)
            PopupCommon.notify('success', '操作成功')
            // if (d.id) {
            //     AjaxCommon("/ccdc/cfg/device/device/edit", "post", d)
            //         .then((res) => {
            //             if (res.data.code == 200) {
            //                 PopupCommon.notify("success", "操作成功")
            //                 self.initPage()
            //                 self.dialogClose()
            //             } else {
            //                 PopupCommon.notify("error", res.data.message)
            //             }
            //         })
            //         .catch((err) => {
            //             PopupCommon.notify("error", err.message)
            //         })
            // } else {
            //     delete d.id
            //     AjaxCommon("/ccdc/cfg/device/device/add", "post", d)
            //         .then((res) => {
            //             if (res.data.code == 200) {
            //                 PopupCommon.notify("success", "操作成功")
            //                 self.initPage()
            //                 self.dialogClose()
            //             } else {
            //                 PopupCommon.notify("error", res.data.message)
            //             }
            //         })
            //         .catch((err) => {
            //             PopupCommon.notify("error", err.message)
            //         })
            // }
        },
        dialogClose() {
            this.dialogShow = false
        },
        searchClick() {
            this.pageNo = 1
            this.initPage()
        },
        resetClick() {
            this.unitName = ''
            this.device_type = ''
        },
        addClick() {
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (res == 'fieldName') {
                        t.filedValue = ''
                    }
                })
            })
            this.formConfig = formConfigItem
            this.type = 'add'
            this.dialogShow = true
            this.dialogTitle = '新增' + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
        },
        // 分页点击事件
        pageChange(p) {
            this.pageNo = p
            this.initPage()
        },
        editRow(d) {
            this.type = 'edit'
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (res == 'fieldName') {
                        t.filedValue = d.data[t[res]]
                    }
                })
            })
            this.formConfig = formConfigItem
            this.dialogShow = true
            this.dialogTitle = '编辑' + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
        },
        viewRow(d) {
            this.type = 'look'
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (res == 'fieldName') {
                        t.filedValue = d.data[t[res]]
                    }
                })
            })
            this.formConfig = formConfigItem
            this.dialogShow = true
            this.dialogTitle = '查看' + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
        },
        deleteRow(d) {
            console.log(d)
            PopupCommon.notify('success', '删除成功')
            // const id = d.data.id
            // let self = this
            // AjaxCommon("/ccdc/cfg/device/device/delete?id=" + id, "delete", {
            //     optReason: d.optReason
            // }).then((res) => {
            //     console.log(res, "res...............")
            //     if (res.data.code == 200) {
            //         PopupCommon.notify("success", "删除成功")
            //         self.initPage()
            //         self.dialogClose()
            //     } else {
            //         PopupCommon.notify("error", res.data.message)
            //     }
            // })
        },
        // 初始化事件
        initPage() {
            //   let self = this;
            //   AjaxCommon("/ccdc/cfg/device/device/listByPage", "post", {
            //     unitName: this.unitName,
            //     deviceStatus: this.status,
            //     deviceType: this.device_type,
            //     pageNo: this.pageNo,
            //     pageSize: this.pageSize,
            //   }).then((res) => {
            //     if (res.data.code == 200) {
            //       self.dataSource = res.data.result.records;
            //       self.pageCount = res.data.result.total;
            //     } else {
            //       PopupCommon.notify("error", res.data.message);
            //     }
            //   });
        }
    },
    mounted() {
        this.initPage()
    }
}
</script>

<style lang="scss" scoped>
.deviceTitle {
    color: #333;
    float: right;
    label {
        font-size: 14px;
        float: left;
        margin-right: 10px;
    }
    img {
        width: 16px;
        float: left;
        margin-top: 10px;
        margin-right: 4px;
    }
}
</style>
